<?php
/**
 * Created by PhpStorm.
 * User: yuyj
 * Date: 16-12-23
 * Time: 下午3:30
 */

namespace common\services\myhtsq;


use common\models\common\ErpUserModel;
use common\repositories\myhtsq\ErpUserRepository;
use common\services\BaseService;
use common\repositories\config\ConfigSettingRepository;
use Yii;

/**
 * Class ErpUserService
 * @package common\services\myhtsq
 */
class ErpUserService extends BaseService
{
    private $repository;

    /**
     * @inheritdoc
     */
    public function __construct(ErpUserRepository $repository, $config = [])
    {
        $this->repository = $repository;
        parent::__construct($config);
    }

    /**
     * 获取用户
     * @param $id
     * @return ErpUserModel
     */
    public function getUserById($id)
    {
        $user = $this->repository->getUserById($id);

        if (empty($user)) {
            return self::setAndReturn(__LINE__, '当前用户不存在');
        }

        //$fields = ['UserGUID', 'UserCode', 'UserName', 'BUGUID', 'Email', 'MobilePhone', 'BUGUID', 'DepartmentGUID'];
        /** @var ErpBusinessUnitService $service */
        $service = Yii::createObject(ErpBusinessUnitService::className());
        $names = $service->getDepartmentNameByDepartmentId([$user->BUGUID, $user->DepartmentGUID]);

        $data = $user->toArray();
        $data['BUName'] = isset($names[$data['BUGUID']]) ? $names[$data['BUGUID']] : null;
        $data['DepartmentName'] = isset($names[$data['DepartmentGUID']]) ? $names[$data['DepartmentGUID']] : null;

        $model = new ErpUserModel();
        $model->setAttributes($data, false);

        return $model;
    }

    /**
     * 获取区域负责人
     * @param $BUGUID
     * @return array
     */
    public function getAreaChargeList($BUGUID)
    {
        return $this->repository->getAreaChargeList($BUGUID);
    }

    /**
     * 获取财务负责人
     * @return array
     */
    public function getFinanceChargeList()
    {
        // 付小玲
//        $userId = 'a3c8f32d-374b-4772-95e9-0f3a84f2f1a1';

        // 高红
//        $userId = '4cea7b02-eaaa-e211-bf86-848f69dfab16';
        /** @var  $configSettingsResp ConfigSettingRepository */
        $configSettingsResp = Yii::createObject(ConfigSettingRepository::className());
        $userId = $configSettingsResp->getValueByKey('finance_charge_GUID');
        $data = $this->getUserById($userId);

        return [$data];
    }

}